package 美团;

import 树.TreeNode;

public class _958_二叉树的完全性检验 {
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) return true;
        int leftDepth = depth(root.left);
        int rightDepth = depth(root.right);
        if (leftDepth == rightDepth) {
            return isCompleteTree(root.left) && isCompleteTree(root.right);
        } else if (leftDepth - 1 == rightDepth) {
            return isCompleteTree(root.left) && isCompleteTree(root.right);
        } else {
            return false;
        }
    }

    private int depth(TreeNode root) {
        if (root == null) return 0;
        return Math.max(depth(root.left), depth(root.right)) + 1;
    }
}
